<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('生产订单列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>订单批号：</label>
                                <input type="text" name="orderCode"/>
                            </li>
                            <li>
                                <label>客户名称：</label>
                                <input type="text" name="clientName"/>
                            </li>
                            <li>
                                <label>产品名称：</label>
                                <input type="text" name="productName"/>
                            </li>
                            <li>
                                <label>产品规格/型号：</label>
                                <input type="text" name="model"/>
                            </li>
                            <li>
                                <label>需求日期：</label>
                                <input type="text" class="time-input" placeholder="请选择需求日期" name="deadtime"/>
                            </li>
                            <li>
                                <label>是否付款：</label>
                                <select name="pay" th:with="type=${@dict.getType('sys_pay_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="process:order:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="process:order:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="process:order:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="process:order:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('process:order:edit')}]];
        var removeFlag = [[${@permission.hasPermi('process:order:remove')}]];
        var applyHuskDatas = [[${@dict.getType('order_husk_apply')}]];
        var payDatas = [[${@dict.getType('sys_pay_status')}]];
        var prefix = ctx + "process/order";
        var succcess_out_storge = '已出库'

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "生产订单",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '订单ID',
                    visible: false
                },
                {
                    field: 'orderCode',
                    title: '订单批号'
                },
                {
                    field: 'clientName',
                    title: '客户名称'
                },
                {
                    field: 'clientPhone',
                    title: '客户电话'
                },
                    {
                        field: 'productId',
                        title: '产品ID'
                    },
                {
                    field: 'productName',
                    title: '产品名称'
                },
                {
                    field: 'model',
                    title: '产品规格/型号'
                },
                {
                    field: 'num',
                    title: '数量'
                },
                {
                    field: 'productUnitname',
                    title: '单位'
                },
                    {
                        field: 'signalPrice',
                        title: '单价'
                    },
                    {
                        field: 'totalPrice',
                        title: '金额'
                    },
                    {
                        field: 'pay',
                        title: '是否付款',
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(payDatas, value);
                        }
                    },
                {
                    field: 'deadtime',
                    title: '需求日期'
                },
                {
                    field: 'bucketLength',
                    title: '箱长度'
                },
                {
                    field: 'bucketWidth',
                    title: '箱宽度'
                },
                {
                    field: 'bucketHeigth',
                    title: '箱高度'
                },
                {
                    field: 'bucketUnitname',
                    title: '单位'
                },
                {
                    field: 'applyHusk',
                    title: '是否需要包皮',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(applyHuskDatas, value);
                    }
                },
                {
                    field: 'huskColor',
                    title: '包皮颜色'
                },
                    {
                        field: 'status',
                        title: '状态'
                    },
                {
                    field: 'common',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        var executeStorageClass = (row.status === succcess_out_storge) ? 'disabled' : '';
                        actions.push('<a id="outStorgeBtn_' + row.id + '" class="btn btn-success btn-xs execute-product-Outstorage ' + executeStorageClass + ' " href="javascript:void(0)" onclick=\'excuteOutStorge(' + JSON.stringify(row).replace(/'/g, "\\'") + ')\' ' + (row.status === succcess_out_storge ? 'disabled' : '') + '><i class="fa fa-edit"></i>销售出库</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function excuteOutStorge(row) {
            layer.confirm('确定要执行销售出库操作吗？', {
                    icon: 3,
                    title: '提示信息'
                },
                function(index) {
                    $.ajax({
                        type: 'POST',
                        url: prefix + '/excuteOutStorge',
                        contentType: 'application/json', // 设置请求头
                        data: JSON.stringify(row),
                        success: function (result) {
                            if(result.code == 0){
                                layer.msg('出库成功');
                                // 更新表格中的状态信息
                                $('#bootstrap-table').bootstrapTable('updateByUniqueId', {
                                    id: id,
                                    row: {
                                        status: succcess_out_storge
                                    }
                                });
                                // 禁用执行入库按钮并改变样式
                                var button = $('#outStorgeBtn_' + id);
                                button.prop('disabled', true);
                                button.addClass('disabled');
                            }
                            else if(result.code == 500){
                                layer.msg('库存不足！');
                            }
                            else{
                                layer.msg('操作失败，请重试');
                            }
                        },
                        error: function (result) {
                            layer.msg('销售出库失败，请重试');
                        }
                    });
                    layer.close(index);
                })

        }
        function getOrderSelections() {
            return $("#" + table.options.id).bootstrapTable('getSelections');
        }
    </script>
</body>
</html>